package cn.java.dao.impl;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import com.sun.org.apache.xml.internal.security.Init;

import cn.java.entity.User;

public class UserDaoImpl {
	
	private SqlSession session=null;
	
	@Before
	public void Init() throws IOException{
		//1.启动mybatis框架 
		//SqlSession <---------SqlSessionFactory <---------SqlSessionFactoryBuilder
		SqlSessionFactoryBuilder sfb=new SqlSessionFactoryBuilder();
		//将mybatis.xml文件转化为输入流
		InputStream inputStream= Resources.getResourceAsStream("mybatis.xml");
		SqlSessionFactory ssf=sfb.build(inputStream);
		session=ssf.openSession();
	}
	
	/**
	 * 获取用户表中的所有数据记录
	 * @throws IOException 
	 */
	@Test
	public void getAllUser() throws IOException{
		//调用局部配置文件中的sql语句(namespace下id为getAllUser的select)
		List<User> userList= session.selectList("cn.java.dao.impl.UserDaoImpl.getAllUser");
		for(User user:userList){
			System.out.println(user);
		}
	}
	
	
	@Test
	public void getAllUser22() throws IOException{
		//调用局部配置文件中的sql语句(namespace下id为getAllUser22的select)
		List<Map<String, Object>> userList= session.selectList("cn.java.dao.impl.UserDaoImpl.getAllUser22");
		for (Map<String, Object> map : userList) {
			System.out.println(map);
		}
	}
	
	@Test
	public void getUserById() throws IOException{
		Long id=1L;//传递参数
		//调用局部配置文件中的sql语句(namespace下id为getUserById的select)
		Map<String, Object> userMap= session.selectOne("cn.java.dao.impl.UserDaoImpl.getUserById",id);
		System.out.println(userMap);
	}
	
	@Test
	public void getUserByCondition() throws IOException{
		//同时传递多个参数
		Map<String, Object> paramMap=new HashMap<String,Object>();
		paramMap.put("username", "王二麻子");
		paramMap.put("pwd", "123");
		//调用局部配置文件中的sql语句(namespace下id为getUserByCondition的select)
		Map<String, Object> userMap= session.selectOne("cn.java.dao.impl.UserDaoImpl.getUserByCondition",paramMap);
		System.out.println(userMap);
	}
	
	@Test
	public void getUserByCondition2() throws IOException{
		//同时传递多个参数
		User user=new User();
		user.setUsername("王二麻子");
		user.setPassword("123");
		//调用局部配置文件中的sql语句(namespace下id为getUserByCondition2的select)
		Map<String, Object> userMap= session.selectOne("cn.java.dao.impl.UserDaoImpl.getUserByCondition2",user);
		System.out.println(userMap);
	}
	
	@Test
	public void getUserByMh() throws IOException{
		//同时传递多个参数
		Map<String, Object>	paramMap=new HashMap<String, Object>();
		paramMap.put("password", "3");
		//调用局部配置文件中的sql语句(namespace下id为getUserByMh的select)
		List<Map<String, Object>> userMap= session.selectList("cn.java.dao.impl.UserDaoImpl.getUserByMh",paramMap);
		for (Map<String, Object> map : userMap) {
			System.out.println(map);
		}
	}
	
	@Test
	public void addUser(){
		Map<String, Object>	paramMap=new HashMap<String, Object>();
		paramMap.put("username", "二狗子");
		paramMap.put("psw", "123");
		//调用局部配置文件中的sql语句(namespace下id为addUser的insert)
		int flag= session.insert("cn.java.dao.impl.UserDaoImpl.addUser", paramMap);
		session.commit();//insert、delete、update必须要提交事务。
		System.out.println(flag);//插入的行数
	}
	
}
